package com.market.cash.ui.activity

import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import com.market.cash.R
import com.market.cash.base.app.CashRootStackManager
import com.market.cash.configs.CashRootConfigs
import com.market.cash.databinding.CashSplashBinding
import com.market.cash.pro.ProActivity
import com.market.cash.ui.presenter.CashRootSplashPresenter
import com.market.cash.ui.view.ICashRootSplashView
import com.market.cash.utils.CashRootBarUtil
import com.market.cash.utils.CashRootImageUtil
import com.market.cash.utils.dialog.CashRootLanguageDialog
import com.market.cash.utils.event.CashRootEventType
import com.market.cash.utils.helper.CashRootAnalysisHelper
import com.market.cash.utils.helper.CashRootCustomConfigsHelper
import com.market.cash.utils.helper.SharedHelper
import com.market.cash.utils.helper.TestDataLibInit
import com.market.cash.utils.language.LocaleContract
import com.market.cash.utils.language.MultiLanguages
import java.util.Locale
import java.util.TimeZone

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/14 19:02
 * @Annotation：启动页
 */
class SplashActivity: ProActivity(),ICashRootSplashView{
    private lateinit var binding: CashSplashBinding
    var presenter: CashRootSplashPresenter? = null

    override fun getCashRootRootView(): View {
        binding = CashSplashBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initCashRootView() {
        CashRootBarUtil.setStatusBarLightMode(this, false)
        isInterceptBack(true)
        //关闭所有页面
        CashRootStackManager.finishAllActivity(SplashActivity::class.java)
        //启动需要重置审核中、放款中显示的公告提示次数
        SharedHelper.saveCashRootSPData(SharedHelper.CashRoot_SPKEY.CASHROOT_DIALOG_NOTICE_NUM,0)
    }

    override fun initCashRootListener() {
        binding.textAppName.setOnClickListener {
            presenter!!.loadCashRootTimeZoneAction()
        }
    }

    override fun initCashRootData() {
        presenter = CashRootSplashPresenter(this)
        presenter!!.loadCashRootTimeZoneAction()

        //牌照信息
        if (!CashRootCustomConfigsHelper.isCashRootShowLicense()){
            binding.licenseLayout.visibility = View.GONE
        } else {
            binding.licenseLayout.visibility = View.VISIBLE
            val licenseInfo = CashRootCustomConfigsHelper.getCashRootLicenseInfo()
            if (licenseInfo != null){
                CashRootImageUtil.instance.loadImageUrl(this,binding.imgLicenseLogo, licenseInfo.optString("logo"))
                val desc = licenseInfo.optString("desc")
                val companyName = licenseInfo.optString("companyName")
                val showText = "$desc $companyName"
                val spString = SpannableString(showText)
                val start = showText.indexOf(companyName)
                spString.setSpan(ForegroundColorSpan(Color.parseColor("#FFFFFF")), start, showText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                binding.textLicenseInfo.text = spString
            }
        }
    }

    /**
     * 显示选择语言
     */
    private fun showCashRootLanguageDialog(){
        val languageDialog = CashRootLanguageDialog(this,"Launch Page",object : CashRootLanguageDialog.IDialogCallback{
            override fun settingLanguageEnglish() {
                saveSelectLanguageAction(LocaleContract.getEnglishLocale())
            }
            override fun settingLanguageSwahili() {
                saveSelectLanguageAction(LocaleContract.getSwahiliLocale())
            }
        })
        languageDialog.setCancelable(false)
        languageDialog.setCanceledOnTouchOutside(false)
        languageDialog.show()
    }
    /**
     * 设置语言
     */
    private fun saveSelectLanguageAction(newLocale: Locale){
        //是否需要重启
        val restart = MultiLanguages.setAppLanguage(this,newLocale)
        if (restart) {
            // 我们可以充分运用 Activity 跳转动画，在跳转的时候设置一个渐变的效果
            onIntent(SplashActivity::class.java)
            overridePendingTransition(R.anim.activity_alpha_in, R.anim.activity_alpha_out)
            finish()
        } else {
            showCashRootLoading()
            presenter!!.loadCashRootConfigsAction()
        }
    }

    /**
     * 时区转发成功
     */
    override fun showCashRootTimeZoneRequestData(dataString: String) {
        var timeZone = ""
        var zoneCode = ""
        var zoneCountry = ""

        dataString.split(",").apply {
            if (this.isNotEmpty()){
                timeZone = this[0].trim()
            }
            if (this.size >= 2){
                zoneCode = this[1].trim()
            }
            if (this.size >= 3){
                zoneCountry = this[2].trim()
            }
        }

        //保存时区id
        SharedHelper.saveCashRootSPData(SharedHelper.CashRoot_SPKEY.CASHROOT_TIMEZONE_ID, TimeZone.getDefault().id)
        //保存转发路径
        SharedHelper.saveCashRootSPData(SharedHelper.CashRoot_SPKEY.CASHROOT_TIMEZONE,timeZone)
        //保存转发地区 区号
        SharedHelper.saveCashRootSPData(SharedHelper.CashRoot_SPKEY.CASHROOT_TIMEZONE_CODE,zoneCode)
        //保存国家标识
        SharedHelper.saveCashRootSPData(SharedHelper.CashRoot_SPKEY.CASHROOT_TIMEZONE_COUNTRY,zoneCountry)

        //初始化analysis lib 赋值 转发地区
        CashRootAnalysisHelper.initAnalysisLibAtZoneSuccess()
        //初始化 data lib
        TestDataLibInit.initDataLibAtZoneSuccess()
        //添加埋点
        addCashRootEventPoint(CashRootEventType.PAGE.CASHROOT_PAGE_LAUNCH_START)

        //是否是坦桑地区
        if (CashRootConfigs.isTanz()){
            if (!SharedHelper.getCashRootSPBoolean(SharedHelper.CashRoot_SPKEY.CASHROOT_LANGUAGE_LAUNCH)){
                hideCashRootLoading()
                showCashRootLanguageDialog()
            } else {
                presenter!!.loadCashRootConfigsAction()
            }
        } else {
            presenter!!.loadCashRootConfigsAction()
        }
    }

    /**
     * 进入权限页
     */
    override fun showCashRootPermissionPage() {
        onIntent(PermissionActivity::class.java)
        finish()
    }

    /**
     * 进入登录页
     */
    override fun showCashRootLoginPage() {
        onIntent(LoginActivity::class.java)
        finish()
    }

    /**
     * 进入主页
     */
    override fun showCashRootMainPage() {
        onIntent(MainActivity::class.java)
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        addCashRootEventPoint(CashRootEventType.PAGE.CASHROOT_PAGE_LAUNCH_END)
    }
}